{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.0\n",
      "1.19.2\n",
      "3.8.6 | packaged by conda-forge | (default, Dec 26 2020, 04:50:20) \n",
      "[Clang 11.0.0 ]\n",
      "1.1.0\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sys\n",
    "import keras\n",
    "import sklearn\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Activation, Embedding, Flatten\n",
    "from keras.layers import LSTM, SimpleRNN, GRU, Bidirectional, BatchNormalization,Convolution1D,MaxPooling1D, Reshape, GlobalAveragePooling1D\n",
    "from tensorflow.keras.utils import to_categorical\n",
    "import sklearn.preprocessing\n",
    "from sklearn import metrics\n",
    "from scipy.stats import zscore\n",
    "from tensorflow.keras.utils import get_file, plot_model\n",
    "from sklearn.model_selection import train_test_split\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "import matplotlib.pyplot as plt\n",
    "print(pd.__version__)\n",
    "print(np.__version__)\n",
    "print(sys.version)\n",
    "print(sklearn.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>dur</th>\n",
       "      <th>proto</th>\n",
       "      <th>service</th>\n",
       "      <th>state</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>...</th>\n",
       "      <th>ct_dst_sport_ltm</th>\n",
       "      <th>ct_dst_src_ltm</th>\n",
       "      <th>is_ftp_login</th>\n",
       "      <th>ct_ftp_cmd</th>\n",
       "      <th>ct_flw_http_mthd</th>\n",
       "      <th>ct_src_ltm</th>\n",
       "      <th>ct_srv_dst</th>\n",
       "      <th>is_sm_ips_ports</th>\n",
       "      <th>attack_cat</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.121478</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>258</td>\n",
       "      <td>172</td>\n",
       "      <td>74.087490</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.649902</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>14</td>\n",
       "      <td>38</td>\n",
       "      <td>734</td>\n",
       "      <td>42014</td>\n",
       "      <td>78.473372</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1.623129</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>8</td>\n",
       "      <td>16</td>\n",
       "      <td>364</td>\n",
       "      <td>13186</td>\n",
       "      <td>14.170161</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1.681642</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp</td>\n",
       "      <td>FIN</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>628</td>\n",
       "      <td>770</td>\n",
       "      <td>13.677108</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.449454</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>534</td>\n",
       "      <td>268</td>\n",
       "      <td>33.373826</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 45 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id       dur proto service state  spkts  dpkts  sbytes  dbytes       rate  \\\n",
       "0   1  0.121478   tcp       -   FIN      6      4     258     172  74.087490   \n",
       "1   2  0.649902   tcp       -   FIN     14     38     734   42014  78.473372   \n",
       "2   3  1.623129   tcp       -   FIN      8     16     364   13186  14.170161   \n",
       "3   4  1.681642   tcp     ftp   FIN     12     12     628     770  13.677108   \n",
       "4   5  0.449454   tcp       -   FIN     10      6     534     268  33.373826   \n",
       "\n",
       "   ...  ct_dst_sport_ltm  ct_dst_src_ltm  is_ftp_login  ct_ftp_cmd  \\\n",
       "0  ...                 1               1             0           0   \n",
       "1  ...                 1               2             0           0   \n",
       "2  ...                 1               3             0           0   \n",
       "3  ...                 1               3             1           1   \n",
       "4  ...                 1              40             0           0   \n",
       "\n",
       "   ct_flw_http_mthd  ct_src_ltm  ct_srv_dst  is_sm_ips_ports  attack_cat  \\\n",
       "0                 0           1           1                0      Normal   \n",
       "1                 0           1           6                0      Normal   \n",
       "2                 0           2           6                0      Normal   \n",
       "3                 0           2           1                0      Normal   \n",
       "4                 0           2          39                0      Normal   \n",
       "\n",
       "   label  \n",
       "0      0  \n",
       "1      0  \n",
       "2      0  \n",
       "3      0  \n",
       "4      0  \n",
       "\n",
       "[5 rows x 45 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Loading training set into dataframe\n",
    "df = pd.read_csv('UNSW_NB15_testing-set.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>dur</th>\n",
       "      <th>proto</th>\n",
       "      <th>service</th>\n",
       "      <th>state</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>...</th>\n",
       "      <th>ct_dst_sport_ltm</th>\n",
       "      <th>ct_dst_src_ltm</th>\n",
       "      <th>is_ftp_login</th>\n",
       "      <th>ct_ftp_cmd</th>\n",
       "      <th>ct_flw_http_mthd</th>\n",
       "      <th>ct_src_ltm</th>\n",
       "      <th>ct_srv_dst</th>\n",
       "      <th>is_sm_ips_ports</th>\n",
       "      <th>attack_cat</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0.000011</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>496</td>\n",
       "      <td>0</td>\n",
       "      <td>90909.090200</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1762</td>\n",
       "      <td>0</td>\n",
       "      <td>125000.000300</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.000005</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1068</td>\n",
       "      <td>0</td>\n",
       "      <td>200000.005100</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>900</td>\n",
       "      <td>0</td>\n",
       "      <td>166666.660800</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2126</td>\n",
       "      <td>0</td>\n",
       "      <td>100000.002500</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>82328</td>\n",
       "      <td>0.000005</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>200000.005100</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>82329</td>\n",
       "      <td>1.106101</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>20</td>\n",
       "      <td>8</td>\n",
       "      <td>18062</td>\n",
       "      <td>354</td>\n",
       "      <td>24.410067</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>82330</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>arp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>82331</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>arp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>82332</td>\n",
       "      <td>0.000009</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>111111.107200</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>82332 rows × 45 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          id       dur proto service state  spkts  dpkts  sbytes  dbytes  \\\n",
       "0          1  0.000011   udp       -   INT      2      0     496       0   \n",
       "1          2  0.000008   udp       -   INT      2      0    1762       0   \n",
       "2          3  0.000005   udp       -   INT      2      0    1068       0   \n",
       "3          4  0.000006   udp       -   INT      2      0     900       0   \n",
       "4          5  0.000010   udp       -   INT      2      0    2126       0   \n",
       "...      ...       ...   ...     ...   ...    ...    ...     ...     ...   \n",
       "82327  82328  0.000005   udp       -   INT      2      0     104       0   \n",
       "82328  82329  1.106101   tcp       -   FIN     20      8   18062     354   \n",
       "82329  82330  0.000000   arp       -   INT      1      0      46       0   \n",
       "82330  82331  0.000000   arp       -   INT      1      0      46       0   \n",
       "82331  82332  0.000009   udp       -   INT      2      0     104       0   \n",
       "\n",
       "                rate  ...  ct_dst_sport_ltm  ct_dst_src_ltm  is_ftp_login  \\\n",
       "0       90909.090200  ...                 1               2             0   \n",
       "1      125000.000300  ...                 1               2             0   \n",
       "2      200000.005100  ...                 1               3             0   \n",
       "3      166666.660800  ...                 1               3             0   \n",
       "4      100000.002500  ...                 1               3             0   \n",
       "...              ...  ...               ...             ...           ...   \n",
       "82327  200000.005100  ...                 1               2             0   \n",
       "82328      24.410067  ...                 1               1             0   \n",
       "82329       0.000000  ...                 1               1             0   \n",
       "82330       0.000000  ...                 1               1             0   \n",
       "82331  111111.107200  ...                 1               1             0   \n",
       "\n",
       "       ct_ftp_cmd  ct_flw_http_mthd  ct_src_ltm  ct_srv_dst  is_sm_ips_ports  \\\n",
       "0               0                 0           1           2                0   \n",
       "1               0                 0           1           2                0   \n",
       "2               0                 0           1           3                0   \n",
       "3               0                 0           2           3                0   \n",
       "4               0                 0           2           3                0   \n",
       "...           ...               ...         ...         ...              ...   \n",
       "82327           0                 0           2           1                0   \n",
       "82328           0                 0           3           2                0   \n",
       "82329           0                 0           1           1                1   \n",
       "82330           0                 0           1           1                1   \n",
       "82331           0                 0           1           1                0   \n",
       "\n",
       "       attack_cat  label  \n",
       "0          Normal      0  \n",
       "1          Normal      0  \n",
       "2          Normal      0  \n",
       "3          Normal      0  \n",
       "4          Normal      0  \n",
       "...           ...    ...  \n",
       "82327      Normal      0  \n",
       "82328      Normal      0  \n",
       "82329      Normal      0  \n",
       "82330      Normal      0  \n",
       "82331      Normal      0  \n",
       "\n",
       "[82332 rows x 45 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Loading testing set into dataframe\n",
    "qp = pd.read_csv('UNSW_NB15_training-set.csv')\n",
    "qp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(175341, 44)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Dropping the last columns of training set\n",
    "df = df.drop('id', 1) # we don't need it in this project\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>proto</th>\n",
       "      <th>service</th>\n",
       "      <th>state</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>...</th>\n",
       "      <th>ct_src_dport_ltm</th>\n",
       "      <th>ct_dst_sport_ltm</th>\n",
       "      <th>ct_dst_src_ltm</th>\n",
       "      <th>is_ftp_login</th>\n",
       "      <th>ct_ftp_cmd</th>\n",
       "      <th>ct_flw_http_mthd</th>\n",
       "      <th>ct_src_ltm</th>\n",
       "      <th>ct_srv_dst</th>\n",
       "      <th>is_sm_ips_ports</th>\n",
       "      <th>attack_cat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.121478</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>258</td>\n",
       "      <td>172</td>\n",
       "      <td>74.087490</td>\n",
       "      <td>252</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.649902</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>14</td>\n",
       "      <td>38</td>\n",
       "      <td>734</td>\n",
       "      <td>42014</td>\n",
       "      <td>78.473372</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.623129</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>8</td>\n",
       "      <td>16</td>\n",
       "      <td>364</td>\n",
       "      <td>13186</td>\n",
       "      <td>14.170161</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.681642</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp</td>\n",
       "      <td>FIN</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>628</td>\n",
       "      <td>770</td>\n",
       "      <td>13.677108</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.449454</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>534</td>\n",
       "      <td>268</td>\n",
       "      <td>33.373826</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        dur proto service state  spkts  dpkts  sbytes  dbytes       rate  \\\n",
       "0  0.121478   tcp       -   FIN      6      4     258     172  74.087490   \n",
       "1  0.649902   tcp       -   FIN     14     38     734   42014  78.473372   \n",
       "2  1.623129   tcp       -   FIN      8     16     364   13186  14.170161   \n",
       "3  1.681642   tcp     ftp   FIN     12     12     628     770  13.677108   \n",
       "4  0.449454   tcp       -   FIN     10      6     534     268  33.373826   \n",
       "\n",
       "   sttl  ...  ct_src_dport_ltm  ct_dst_sport_ltm  ct_dst_src_ltm  \\\n",
       "0   252  ...                 1                 1               1   \n",
       "1    62  ...                 1                 1               2   \n",
       "2    62  ...                 1                 1               3   \n",
       "3    62  ...                 1                 1               3   \n",
       "4   254  ...                 2                 1              40   \n",
       "\n",
       "   is_ftp_login  ct_ftp_cmd  ct_flw_http_mthd  ct_src_ltm  ct_srv_dst  \\\n",
       "0             0           0                 0           1           1   \n",
       "1             0           0                 0           1           6   \n",
       "2             0           0                 0           2           6   \n",
       "3             1           1                 0           2           1   \n",
       "4             0           0                 0           2          39   \n",
       "\n",
       "   is_sm_ips_ports  attack_cat  \n",
       "0                0      Normal  \n",
       "1                0      Normal  \n",
       "2                0      Normal  \n",
       "3                0      Normal  \n",
       "4                0      Normal  \n",
       "\n",
       "[5 rows x 43 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.drop('label', 1) # we don't need it in this project\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(82332, 43)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Dropping the last columns of testing set\n",
    "qp = qp.drop('id', 1)\n",
    "qp = qp.drop('label', 1)\n",
    "qp.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qp.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['proto', 'state', 'service']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#defining col list\n",
    "cols = ['proto','state','service']\n",
    "cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#One-hot encoding\n",
    "def one_hot(df, cols):\n",
    "    \"\"\"\n",
    "    @param df pandas DataFrame\n",
    "    @param cols a list of columns to encode\n",
    "    @return a DataFrame with one-hot encoding\n",
    "    \"\"\"\n",
    "    for each in cols:\n",
    "        dummies = pd.get_dummies(df[each], prefix=each, drop_first=False)\n",
    "        df = pd.concat([df, dummies], axis=1)\n",
    "        df = df.drop(each, 1)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>proto</th>\n",
       "      <th>service</th>\n",
       "      <th>state</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>...</th>\n",
       "      <th>ct_src_dport_ltm</th>\n",
       "      <th>ct_dst_sport_ltm</th>\n",
       "      <th>ct_dst_src_ltm</th>\n",
       "      <th>is_ftp_login</th>\n",
       "      <th>ct_ftp_cmd</th>\n",
       "      <th>ct_flw_http_mthd</th>\n",
       "      <th>ct_src_ltm</th>\n",
       "      <th>ct_srv_dst</th>\n",
       "      <th>is_sm_ips_ports</th>\n",
       "      <th>attack_cat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.121478</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>258</td>\n",
       "      <td>172</td>\n",
       "      <td>74.087490</td>\n",
       "      <td>252</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.649902</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>14</td>\n",
       "      <td>38</td>\n",
       "      <td>734</td>\n",
       "      <td>42014</td>\n",
       "      <td>78.473372</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.623129</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>8</td>\n",
       "      <td>16</td>\n",
       "      <td>364</td>\n",
       "      <td>13186</td>\n",
       "      <td>14.170161</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.681642</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp</td>\n",
       "      <td>FIN</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>628</td>\n",
       "      <td>770</td>\n",
       "      <td>13.677108</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.449454</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>534</td>\n",
       "      <td>268</td>\n",
       "      <td>33.373826</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>0.000005</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>200000.005100</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>1.106101</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>20</td>\n",
       "      <td>8</td>\n",
       "      <td>18062</td>\n",
       "      <td>354</td>\n",
       "      <td>24.410067</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>arp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>arp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>0.000009</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>111111.107200</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257673 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            dur proto service state  spkts  dpkts  sbytes  dbytes  \\\n",
       "0      0.121478   tcp       -   FIN      6      4     258     172   \n",
       "1      0.649902   tcp       -   FIN     14     38     734   42014   \n",
       "2      1.623129   tcp       -   FIN      8     16     364   13186   \n",
       "3      1.681642   tcp     ftp   FIN     12     12     628     770   \n",
       "4      0.449454   tcp       -   FIN     10      6     534     268   \n",
       "...         ...   ...     ...   ...    ...    ...     ...     ...   \n",
       "82327  0.000005   udp       -   INT      2      0     104       0   \n",
       "82328  1.106101   tcp       -   FIN     20      8   18062     354   \n",
       "82329  0.000000   arp       -   INT      1      0      46       0   \n",
       "82330  0.000000   arp       -   INT      1      0      46       0   \n",
       "82331  0.000009   udp       -   INT      2      0     104       0   \n",
       "\n",
       "                rate  sttl  ...  ct_src_dport_ltm  ct_dst_sport_ltm  \\\n",
       "0          74.087490   252  ...                 1                 1   \n",
       "1          78.473372    62  ...                 1                 1   \n",
       "2          14.170161    62  ...                 1                 1   \n",
       "3          13.677108    62  ...                 1                 1   \n",
       "4          33.373826   254  ...                 2                 1   \n",
       "...              ...   ...  ...               ...               ...   \n",
       "82327  200000.005100   254  ...                 1                 1   \n",
       "82328      24.410067   254  ...                 1                 1   \n",
       "82329       0.000000     0  ...                 1                 1   \n",
       "82330       0.000000     0  ...                 1                 1   \n",
       "82331  111111.107200   254  ...                 1                 1   \n",
       "\n",
       "       ct_dst_src_ltm  is_ftp_login  ct_ftp_cmd  ct_flw_http_mthd  ct_src_ltm  \\\n",
       "0                   1             0           0                 0           1   \n",
       "1                   2             0           0                 0           1   \n",
       "2                   3             0           0                 0           2   \n",
       "3                   3             1           1                 0           2   \n",
       "4                  40             0           0                 0           2   \n",
       "...               ...           ...         ...               ...         ...   \n",
       "82327               2             0           0                 0           2   \n",
       "82328               1             0           0                 0           3   \n",
       "82329               1             0           0                 0           1   \n",
       "82330               1             0           0                 0           1   \n",
       "82331               1             0           0                 0           1   \n",
       "\n",
       "       ct_srv_dst  is_sm_ips_ports  attack_cat  \n",
       "0               1                0      Normal  \n",
       "1               6                0      Normal  \n",
       "2               6                0      Normal  \n",
       "3               1                0      Normal  \n",
       "4              39                0      Normal  \n",
       "...           ...              ...         ...  \n",
       "82327           1                0      Normal  \n",
       "82328           2                0      Normal  \n",
       "82329           1                1      Normal  \n",
       "82330           1                1      Normal  \n",
       "82331           1                0      Normal  \n",
       "\n",
       "[257673 rows x 43 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Merging train and test data\n",
    "combined_data = pd.concat([df,qp])\n",
    "combined_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>proto</th>\n",
       "      <th>service</th>\n",
       "      <th>state</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>...</th>\n",
       "      <th>ct_src_dport_ltm</th>\n",
       "      <th>ct_dst_sport_ltm</th>\n",
       "      <th>ct_dst_src_ltm</th>\n",
       "      <th>is_ftp_login</th>\n",
       "      <th>ct_ftp_cmd</th>\n",
       "      <th>ct_flw_http_mthd</th>\n",
       "      <th>ct_src_ltm</th>\n",
       "      <th>ct_srv_dst</th>\n",
       "      <th>is_sm_ips_ports</th>\n",
       "      <th>attack_cat</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.121478</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>258</td>\n",
       "      <td>172</td>\n",
       "      <td>74.087490</td>\n",
       "      <td>252</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.649902</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>14</td>\n",
       "      <td>38</td>\n",
       "      <td>734</td>\n",
       "      <td>42014</td>\n",
       "      <td>78.473372</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.623129</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>8</td>\n",
       "      <td>16</td>\n",
       "      <td>364</td>\n",
       "      <td>13186</td>\n",
       "      <td>14.170161</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.681642</td>\n",
       "      <td>tcp</td>\n",
       "      <td>ftp</td>\n",
       "      <td>FIN</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>628</td>\n",
       "      <td>770</td>\n",
       "      <td>13.677108</td>\n",
       "      <td>62</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.449454</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>534</td>\n",
       "      <td>268</td>\n",
       "      <td>33.373826</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>0.000005</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>200000.005100</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>1.106101</td>\n",
       "      <td>tcp</td>\n",
       "      <td>-</td>\n",
       "      <td>FIN</td>\n",
       "      <td>20</td>\n",
       "      <td>8</td>\n",
       "      <td>18062</td>\n",
       "      <td>354</td>\n",
       "      <td>24.410067</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>arp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>arp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>0.000009</td>\n",
       "      <td>udp</td>\n",
       "      <td>-</td>\n",
       "      <td>INT</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>111111.107200</td>\n",
       "      <td>254</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257673 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            dur proto service state  spkts  dpkts  sbytes  dbytes  \\\n",
       "0      0.121478   tcp       -   FIN      6      4     258     172   \n",
       "1      0.649902   tcp       -   FIN     14     38     734   42014   \n",
       "2      1.623129   tcp       -   FIN      8     16     364   13186   \n",
       "3      1.681642   tcp     ftp   FIN     12     12     628     770   \n",
       "4      0.449454   tcp       -   FIN     10      6     534     268   \n",
       "...         ...   ...     ...   ...    ...    ...     ...     ...   \n",
       "82327  0.000005   udp       -   INT      2      0     104       0   \n",
       "82328  1.106101   tcp       -   FIN     20      8   18062     354   \n",
       "82329  0.000000   arp       -   INT      1      0      46       0   \n",
       "82330  0.000000   arp       -   INT      1      0      46       0   \n",
       "82331  0.000009   udp       -   INT      2      0     104       0   \n",
       "\n",
       "                rate  sttl  ...  ct_src_dport_ltm  ct_dst_sport_ltm  \\\n",
       "0          74.087490   252  ...                 1                 1   \n",
       "1          78.473372    62  ...                 1                 1   \n",
       "2          14.170161    62  ...                 1                 1   \n",
       "3          13.677108    62  ...                 1                 1   \n",
       "4          33.373826   254  ...                 2                 1   \n",
       "...              ...   ...  ...               ...               ...   \n",
       "82327  200000.005100   254  ...                 1                 1   \n",
       "82328      24.410067   254  ...                 1                 1   \n",
       "82329       0.000000     0  ...                 1                 1   \n",
       "82330       0.000000     0  ...                 1                 1   \n",
       "82331  111111.107200   254  ...                 1                 1   \n",
       "\n",
       "       ct_dst_src_ltm  is_ftp_login  ct_ftp_cmd  ct_flw_http_mthd  ct_src_ltm  \\\n",
       "0                   1             0           0                 0           1   \n",
       "1                   2             0           0                 0           1   \n",
       "2                   3             0           0                 0           2   \n",
       "3                   3             1           1                 0           2   \n",
       "4                  40             0           0                 0           2   \n",
       "...               ...           ...         ...               ...         ...   \n",
       "82327               2             0           0                 0           2   \n",
       "82328               1             0           0                 0           3   \n",
       "82329               1             0           0                 0           1   \n",
       "82330               1             0           0                 0           1   \n",
       "82331               1             0           0                 0           1   \n",
       "\n",
       "       ct_srv_dst  is_sm_ips_ports  attack_cat  \n",
       "0               1                0      Normal  \n",
       "1               6                0      Normal  \n",
       "2               6                0      Normal  \n",
       "3               1                0      Normal  \n",
       "4              39                0      Normal  \n",
       "...           ...              ...         ...  \n",
       "82327           1                0      Normal  \n",
       "82328           2                0      Normal  \n",
       "82329           1                1      Normal  \n",
       "82330           1                1      Normal  \n",
       "82331           1                0      Normal  \n",
       "\n",
       "[257673 rows x 43 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combined_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = combined_data.pop('attack_cat')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Applying one hot encoding to combined data\n",
    "combined_data = one_hot(combined_data,cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>dttl</th>\n",
       "      <th>sload</th>\n",
       "      <th>dload</th>\n",
       "      <th>...</th>\n",
       "      <th>service_ftp</th>\n",
       "      <th>service_ftp-data</th>\n",
       "      <th>service_http</th>\n",
       "      <th>service_irc</th>\n",
       "      <th>service_pop3</th>\n",
       "      <th>service_radius</th>\n",
       "      <th>service_smtp</th>\n",
       "      <th>service_snmp</th>\n",
       "      <th>service_ssh</th>\n",
       "      <th>service_ssl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.121478</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>258</td>\n",
       "      <td>172</td>\n",
       "      <td>74.087490</td>\n",
       "      <td>252</td>\n",
       "      <td>254</td>\n",
       "      <td>1.415894e+04</td>\n",
       "      <td>8495.365234</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.649902</td>\n",
       "      <td>14</td>\n",
       "      <td>38</td>\n",
       "      <td>734</td>\n",
       "      <td>42014</td>\n",
       "      <td>78.473372</td>\n",
       "      <td>62</td>\n",
       "      <td>252</td>\n",
       "      <td>8.395112e+03</td>\n",
       "      <td>503571.312500</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.623129</td>\n",
       "      <td>8</td>\n",
       "      <td>16</td>\n",
       "      <td>364</td>\n",
       "      <td>13186</td>\n",
       "      <td>14.170161</td>\n",
       "      <td>62</td>\n",
       "      <td>252</td>\n",
       "      <td>1.572272e+03</td>\n",
       "      <td>60929.230470</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.681642</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>628</td>\n",
       "      <td>770</td>\n",
       "      <td>13.677108</td>\n",
       "      <td>62</td>\n",
       "      <td>252</td>\n",
       "      <td>2.740179e+03</td>\n",
       "      <td>3358.622070</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.449454</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>534</td>\n",
       "      <td>268</td>\n",
       "      <td>33.373826</td>\n",
       "      <td>254</td>\n",
       "      <td>252</td>\n",
       "      <td>8.561499e+03</td>\n",
       "      <td>3987.059814</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>0.000005</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>200000.005100</td>\n",
       "      <td>254</td>\n",
       "      <td>0</td>\n",
       "      <td>8.320000e+07</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>1.106101</td>\n",
       "      <td>20</td>\n",
       "      <td>8</td>\n",
       "      <td>18062</td>\n",
       "      <td>354</td>\n",
       "      <td>24.410067</td>\n",
       "      <td>254</td>\n",
       "      <td>252</td>\n",
       "      <td>1.241044e+05</td>\n",
       "      <td>2242.109863</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>0.000009</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>0</td>\n",
       "      <td>111111.107200</td>\n",
       "      <td>254</td>\n",
       "      <td>0</td>\n",
       "      <td>4.622222e+07</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257673 rows × 196 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            dur  spkts  dpkts  sbytes  dbytes           rate  sttl  dttl  \\\n",
       "0      0.121478      6      4     258     172      74.087490   252   254   \n",
       "1      0.649902     14     38     734   42014      78.473372    62   252   \n",
       "2      1.623129      8     16     364   13186      14.170161    62   252   \n",
       "3      1.681642     12     12     628     770      13.677108    62   252   \n",
       "4      0.449454     10      6     534     268      33.373826   254   252   \n",
       "...         ...    ...    ...     ...     ...            ...   ...   ...   \n",
       "82327  0.000005      2      0     104       0  200000.005100   254     0   \n",
       "82328  1.106101     20      8   18062     354      24.410067   254   252   \n",
       "82329  0.000000      1      0      46       0       0.000000     0     0   \n",
       "82330  0.000000      1      0      46       0       0.000000     0     0   \n",
       "82331  0.000009      2      0     104       0  111111.107200   254     0   \n",
       "\n",
       "              sload          dload  ...  service_ftp  service_ftp-data  \\\n",
       "0      1.415894e+04    8495.365234  ...            0                 0   \n",
       "1      8.395112e+03  503571.312500  ...            0                 0   \n",
       "2      1.572272e+03   60929.230470  ...            0                 0   \n",
       "3      2.740179e+03    3358.622070  ...            1                 0   \n",
       "4      8.561499e+03    3987.059814  ...            0                 0   \n",
       "...             ...            ...  ...          ...               ...   \n",
       "82327  8.320000e+07       0.000000  ...            0                 0   \n",
       "82328  1.241044e+05    2242.109863  ...            0                 0   \n",
       "82329  0.000000e+00       0.000000  ...            0                 0   \n",
       "82330  0.000000e+00       0.000000  ...            0                 0   \n",
       "82331  4.622222e+07       0.000000  ...            0                 0   \n",
       "\n",
       "       service_http  service_irc  service_pop3  service_radius  service_smtp  \\\n",
       "0                 0            0             0               0             0   \n",
       "1                 0            0             0               0             0   \n",
       "2                 0            0             0               0             0   \n",
       "3                 0            0             0               0             0   \n",
       "4                 0            0             0               0             0   \n",
       "...             ...          ...           ...             ...           ...   \n",
       "82327             0            0             0               0             0   \n",
       "82328             0            0             0               0             0   \n",
       "82329             0            0             0               0             0   \n",
       "82330             0            0             0               0             0   \n",
       "82331             0            0             0               0             0   \n",
       "\n",
       "       service_snmp  service_ssh  service_ssl  \n",
       "0                 0            0            0  \n",
       "1                 0            0            0  \n",
       "2                 0            0            0  \n",
       "3                 0            0            0  \n",
       "4                 0            0            0  \n",
       "...             ...          ...          ...  \n",
       "82327             0            0            0  \n",
       "82328             0            0            0  \n",
       "82329             0            0            0  \n",
       "82330             0            0            0  \n",
       "82331             0            0            0  \n",
       "\n",
       "[257673 rows x 196 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combined_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Function to min-max normalize\n",
    "def normalize(df, cols):\n",
    "    \"\"\"\n",
    "    @param df pandas DataFrame\n",
    "    @param cols a list of columns to encode\n",
    "    @return a DataFrame with normalized specified features\n",
    "    \"\"\"\n",
    "    result = df.copy() # do not touch the original df\n",
    "    for feature_name in cols:\n",
    "        max_value = df[feature_name].max()\n",
    "        min_value = df[feature_name].min()\n",
    "        if max_value > min_value:\n",
    "            result[feature_name] = (df[feature_name] - min_value) / (max_value - min_value)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>dttl</th>\n",
       "      <th>sload</th>\n",
       "      <th>dload</th>\n",
       "      <th>...</th>\n",
       "      <th>service_ftp</th>\n",
       "      <th>service_ftp-data</th>\n",
       "      <th>service_http</th>\n",
       "      <th>service_irc</th>\n",
       "      <th>service_pop3</th>\n",
       "      <th>service_radius</th>\n",
       "      <th>service_smtp</th>\n",
       "      <th>service_snmp</th>\n",
       "      <th>service_ssh</th>\n",
       "      <th>service_ssl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.024634e-03</td>\n",
       "      <td>0.000470</td>\n",
       "      <td>0.000363</td>\n",
       "      <td>0.000016</td>\n",
       "      <td>0.000012</td>\n",
       "      <td>0.000074</td>\n",
       "      <td>0.988235</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.364553e-06</td>\n",
       "      <td>0.000379</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.083170e-02</td>\n",
       "      <td>0.001221</td>\n",
       "      <td>0.003449</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002866</td>\n",
       "      <td>0.000078</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>1.401989e-06</td>\n",
       "      <td>0.022458</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.705215e-02</td>\n",
       "      <td>0.000658</td>\n",
       "      <td>0.001452</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.000900</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>2.625704e-07</td>\n",
       "      <td>0.002717</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.802737e-02</td>\n",
       "      <td>0.001033</td>\n",
       "      <td>0.001089</td>\n",
       "      <td>0.000042</td>\n",
       "      <td>0.000053</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>4.576117e-07</td>\n",
       "      <td>0.000150</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7.490901e-03</td>\n",
       "      <td>0.000845</td>\n",
       "      <td>0.000545</td>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000018</td>\n",
       "      <td>0.000033</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>1.429776e-06</td>\n",
       "      <td>0.000178</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>8.333335e-08</td>\n",
       "      <td>0.000094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.389445e-02</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>1.843502e-02</td>\n",
       "      <td>0.001785</td>\n",
       "      <td>0.000726</td>\n",
       "      <td>0.001257</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>2.072552e-05</td>\n",
       "      <td>0.000100</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>1.500000e-07</td>\n",
       "      <td>0.000094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.719141e-03</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257673 rows × 196 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                dur     spkts     dpkts    sbytes    dbytes      rate  \\\n",
       "0      2.024634e-03  0.000470  0.000363  0.000016  0.000012  0.000074   \n",
       "1      1.083170e-02  0.001221  0.003449  0.000049  0.002866  0.000078   \n",
       "2      2.705215e-02  0.000658  0.001452  0.000024  0.000900  0.000014   \n",
       "3      2.802737e-02  0.001033  0.001089  0.000042  0.000053  0.000014   \n",
       "4      7.490901e-03  0.000845  0.000545  0.000036  0.000018  0.000033   \n",
       "...             ...       ...       ...       ...       ...       ...   \n",
       "82327  8.333335e-08  0.000094  0.000000  0.000006  0.000000  0.200000   \n",
       "82328  1.843502e-02  0.001785  0.000726  0.001257  0.000024  0.000024   \n",
       "82329  0.000000e+00  0.000000  0.000000  0.000002  0.000000  0.000000   \n",
       "82330  0.000000e+00  0.000000  0.000000  0.000002  0.000000  0.000000   \n",
       "82331  1.500000e-07  0.000094  0.000000  0.000006  0.000000  0.111111   \n",
       "\n",
       "           sttl      dttl         sload     dload  ...  service_ftp  \\\n",
       "0      0.988235  1.000000  2.364553e-06  0.000379  ...          0.0   \n",
       "1      0.243137  0.992126  1.401989e-06  0.022458  ...          0.0   \n",
       "2      0.243137  0.992126  2.625704e-07  0.002717  ...          0.0   \n",
       "3      0.243137  0.992126  4.576117e-07  0.000150  ...          1.0   \n",
       "4      0.996078  0.992126  1.429776e-06  0.000178  ...          0.0   \n",
       "...         ...       ...           ...       ...  ...          ...   \n",
       "82327  0.996078  0.000000  1.389445e-02  0.000000  ...          0.0   \n",
       "82328  0.996078  0.992126  2.072552e-05  0.000100  ...          0.0   \n",
       "82329  0.000000  0.000000  0.000000e+00  0.000000  ...          0.0   \n",
       "82330  0.000000  0.000000  0.000000e+00  0.000000  ...          0.0   \n",
       "82331  0.996078  0.000000  7.719141e-03  0.000000  ...          0.0   \n",
       "\n",
       "       service_ftp-data  service_http  service_irc  service_pop3  \\\n",
       "0                   0.0           0.0          0.0           0.0   \n",
       "1                   0.0           0.0          0.0           0.0   \n",
       "2                   0.0           0.0          0.0           0.0   \n",
       "3                   0.0           0.0          0.0           0.0   \n",
       "4                   0.0           0.0          0.0           0.0   \n",
       "...                 ...           ...          ...           ...   \n",
       "82327               0.0           0.0          0.0           0.0   \n",
       "82328               0.0           0.0          0.0           0.0   \n",
       "82329               0.0           0.0          0.0           0.0   \n",
       "82330               0.0           0.0          0.0           0.0   \n",
       "82331               0.0           0.0          0.0           0.0   \n",
       "\n",
       "       service_radius  service_smtp  service_snmp  service_ssh  service_ssl  \n",
       "0                 0.0           0.0           0.0          0.0          0.0  \n",
       "1                 0.0           0.0           0.0          0.0          0.0  \n",
       "2                 0.0           0.0           0.0          0.0          0.0  \n",
       "3                 0.0           0.0           0.0          0.0          0.0  \n",
       "4                 0.0           0.0           0.0          0.0          0.0  \n",
       "...               ...           ...           ...          ...          ...  \n",
       "82327             0.0           0.0           0.0          0.0          0.0  \n",
       "82328             0.0           0.0           0.0          0.0          0.0  \n",
       "82329             0.0           0.0           0.0          0.0          0.0  \n",
       "82330             0.0           0.0           0.0          0.0          0.0  \n",
       "82331             0.0           0.0           0.0          0.0          0.0  \n",
       "\n",
       "[257673 rows x 196 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Normalizing training set\n",
    "new_train_df = normalize(combined_data,combined_data.columns)\n",
    "new_train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        Normal\n",
       "1        Normal\n",
       "2        Normal\n",
       "3        Normal\n",
       "4        Normal\n",
       "          ...  \n",
       "82327    Normal\n",
       "82328    Normal\n",
       "82329    Normal\n",
       "82330    Normal\n",
       "82331    Normal\n",
       "Name: attack_cat, Length: 257673, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>dttl</th>\n",
       "      <th>sload</th>\n",
       "      <th>dload</th>\n",
       "      <th>...</th>\n",
       "      <th>service_ftp-data</th>\n",
       "      <th>service_http</th>\n",
       "      <th>service_irc</th>\n",
       "      <th>service_pop3</th>\n",
       "      <th>service_radius</th>\n",
       "      <th>service_smtp</th>\n",
       "      <th>service_snmp</th>\n",
       "      <th>service_ssh</th>\n",
       "      <th>service_ssl</th>\n",
       "      <th>Class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.024634e-03</td>\n",
       "      <td>0.000470</td>\n",
       "      <td>0.000363</td>\n",
       "      <td>0.000016</td>\n",
       "      <td>0.000012</td>\n",
       "      <td>0.000074</td>\n",
       "      <td>0.988235</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.364553e-06</td>\n",
       "      <td>0.000379</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.083170e-02</td>\n",
       "      <td>0.001221</td>\n",
       "      <td>0.003449</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002866</td>\n",
       "      <td>0.000078</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>1.401989e-06</td>\n",
       "      <td>0.022458</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.705215e-02</td>\n",
       "      <td>0.000658</td>\n",
       "      <td>0.001452</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.000900</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>2.625704e-07</td>\n",
       "      <td>0.002717</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.802737e-02</td>\n",
       "      <td>0.001033</td>\n",
       "      <td>0.001089</td>\n",
       "      <td>0.000042</td>\n",
       "      <td>0.000053</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>4.576117e-07</td>\n",
       "      <td>0.000150</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7.490901e-03</td>\n",
       "      <td>0.000845</td>\n",
       "      <td>0.000545</td>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000018</td>\n",
       "      <td>0.000033</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>1.429776e-06</td>\n",
       "      <td>0.000178</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>8.333335e-08</td>\n",
       "      <td>0.000094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.389445e-02</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>1.843502e-02</td>\n",
       "      <td>0.001785</td>\n",
       "      <td>0.000726</td>\n",
       "      <td>0.001257</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>2.072552e-05</td>\n",
       "      <td>0.000100</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>1.500000e-07</td>\n",
       "      <td>0.000094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.719141e-03</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257673 rows × 197 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                dur     spkts     dpkts    sbytes    dbytes      rate  \\\n",
       "0      2.024634e-03  0.000470  0.000363  0.000016  0.000012  0.000074   \n",
       "1      1.083170e-02  0.001221  0.003449  0.000049  0.002866  0.000078   \n",
       "2      2.705215e-02  0.000658  0.001452  0.000024  0.000900  0.000014   \n",
       "3      2.802737e-02  0.001033  0.001089  0.000042  0.000053  0.000014   \n",
       "4      7.490901e-03  0.000845  0.000545  0.000036  0.000018  0.000033   \n",
       "...             ...       ...       ...       ...       ...       ...   \n",
       "82327  8.333335e-08  0.000094  0.000000  0.000006  0.000000  0.200000   \n",
       "82328  1.843502e-02  0.001785  0.000726  0.001257  0.000024  0.000024   \n",
       "82329  0.000000e+00  0.000000  0.000000  0.000002  0.000000  0.000000   \n",
       "82330  0.000000e+00  0.000000  0.000000  0.000002  0.000000  0.000000   \n",
       "82331  1.500000e-07  0.000094  0.000000  0.000006  0.000000  0.111111   \n",
       "\n",
       "           sttl      dttl         sload     dload  ...  service_ftp-data  \\\n",
       "0      0.988235  1.000000  2.364553e-06  0.000379  ...               0.0   \n",
       "1      0.243137  0.992126  1.401989e-06  0.022458  ...               0.0   \n",
       "2      0.243137  0.992126  2.625704e-07  0.002717  ...               0.0   \n",
       "3      0.243137  0.992126  4.576117e-07  0.000150  ...               0.0   \n",
       "4      0.996078  0.992126  1.429776e-06  0.000178  ...               0.0   \n",
       "...         ...       ...           ...       ...  ...               ...   \n",
       "82327  0.996078  0.000000  1.389445e-02  0.000000  ...               0.0   \n",
       "82328  0.996078  0.992126  2.072552e-05  0.000100  ...               0.0   \n",
       "82329  0.000000  0.000000  0.000000e+00  0.000000  ...               0.0   \n",
       "82330  0.000000  0.000000  0.000000e+00  0.000000  ...               0.0   \n",
       "82331  0.996078  0.000000  7.719141e-03  0.000000  ...               0.0   \n",
       "\n",
       "       service_http  service_irc  service_pop3  service_radius  service_smtp  \\\n",
       "0               0.0          0.0           0.0             0.0           0.0   \n",
       "1               0.0          0.0           0.0             0.0           0.0   \n",
       "2               0.0          0.0           0.0             0.0           0.0   \n",
       "3               0.0          0.0           0.0             0.0           0.0   \n",
       "4               0.0          0.0           0.0             0.0           0.0   \n",
       "...             ...          ...           ...             ...           ...   \n",
       "82327           0.0          0.0           0.0             0.0           0.0   \n",
       "82328           0.0          0.0           0.0             0.0           0.0   \n",
       "82329           0.0          0.0           0.0             0.0           0.0   \n",
       "82330           0.0          0.0           0.0             0.0           0.0   \n",
       "82331           0.0          0.0           0.0             0.0           0.0   \n",
       "\n",
       "       service_snmp  service_ssh  service_ssl   Class  \n",
       "0               0.0          0.0          0.0  Normal  \n",
       "1               0.0          0.0          0.0  Normal  \n",
       "2               0.0          0.0          0.0  Normal  \n",
       "3               0.0          0.0          0.0  Normal  \n",
       "4               0.0          0.0          0.0  Normal  \n",
       "...             ...          ...          ...     ...  \n",
       "82327           0.0          0.0          0.0  Normal  \n",
       "82328           0.0          0.0          0.0  Normal  \n",
       "82329           0.0          0.0          0.0  Normal  \n",
       "82330           0.0          0.0          0.0  Normal  \n",
       "82331           0.0          0.0          0.0  Normal  \n",
       "\n",
       "[257673 rows x 197 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Appending class column to training set\n",
    "new_train_df[\"Class\"] = tmp\n",
    "new_train_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_train_df.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        Normal\n",
       "1        Normal\n",
       "2        Normal\n",
       "3        Normal\n",
       "4        Normal\n",
       "          ...  \n",
       "82327    Normal\n",
       "82328    Normal\n",
       "82329    Normal\n",
       "82330    Normal\n",
       "82331    Normal\n",
       "Name: Class, Length: 257673, dtype: object"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train=new_train_df[\"Class\"]\n",
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dur</th>\n",
       "      <th>spkts</th>\n",
       "      <th>dpkts</th>\n",
       "      <th>sbytes</th>\n",
       "      <th>dbytes</th>\n",
       "      <th>rate</th>\n",
       "      <th>sttl</th>\n",
       "      <th>dttl</th>\n",
       "      <th>sload</th>\n",
       "      <th>dload</th>\n",
       "      <th>...</th>\n",
       "      <th>service_ftp</th>\n",
       "      <th>service_ftp-data</th>\n",
       "      <th>service_http</th>\n",
       "      <th>service_irc</th>\n",
       "      <th>service_pop3</th>\n",
       "      <th>service_radius</th>\n",
       "      <th>service_smtp</th>\n",
       "      <th>service_snmp</th>\n",
       "      <th>service_ssh</th>\n",
       "      <th>service_ssl</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.024634e-03</td>\n",
       "      <td>0.000470</td>\n",
       "      <td>0.000363</td>\n",
       "      <td>0.000016</td>\n",
       "      <td>0.000012</td>\n",
       "      <td>0.000074</td>\n",
       "      <td>0.988235</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.364553e-06</td>\n",
       "      <td>0.000379</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.083170e-02</td>\n",
       "      <td>0.001221</td>\n",
       "      <td>0.003449</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002866</td>\n",
       "      <td>0.000078</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>1.401989e-06</td>\n",
       "      <td>0.022458</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.705215e-02</td>\n",
       "      <td>0.000658</td>\n",
       "      <td>0.001452</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.000900</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>2.625704e-07</td>\n",
       "      <td>0.002717</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.802737e-02</td>\n",
       "      <td>0.001033</td>\n",
       "      <td>0.001089</td>\n",
       "      <td>0.000042</td>\n",
       "      <td>0.000053</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.243137</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>4.576117e-07</td>\n",
       "      <td>0.000150</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7.490901e-03</td>\n",
       "      <td>0.000845</td>\n",
       "      <td>0.000545</td>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000018</td>\n",
       "      <td>0.000033</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>1.429776e-06</td>\n",
       "      <td>0.000178</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82327</th>\n",
       "      <td>8.333335e-08</td>\n",
       "      <td>0.000094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.389445e-02</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82328</th>\n",
       "      <td>1.843502e-02</td>\n",
       "      <td>0.001785</td>\n",
       "      <td>0.000726</td>\n",
       "      <td>0.001257</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.000024</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.992126</td>\n",
       "      <td>2.072552e-05</td>\n",
       "      <td>0.000100</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82329</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82330</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82331</th>\n",
       "      <td>1.500000e-07</td>\n",
       "      <td>0.000094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000006</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.996078</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.719141e-03</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>257673 rows × 196 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                dur     spkts     dpkts    sbytes    dbytes      rate  \\\n",
       "0      2.024634e-03  0.000470  0.000363  0.000016  0.000012  0.000074   \n",
       "1      1.083170e-02  0.001221  0.003449  0.000049  0.002866  0.000078   \n",
       "2      2.705215e-02  0.000658  0.001452  0.000024  0.000900  0.000014   \n",
       "3      2.802737e-02  0.001033  0.001089  0.000042  0.000053  0.000014   \n",
       "4      7.490901e-03  0.000845  0.000545  0.000036  0.000018  0.000033   \n",
       "...             ...       ...       ...       ...       ...       ...   \n",
       "82327  8.333335e-08  0.000094  0.000000  0.000006  0.000000  0.200000   \n",
       "82328  1.843502e-02  0.001785  0.000726  0.001257  0.000024  0.000024   \n",
       "82329  0.000000e+00  0.000000  0.000000  0.000002  0.000000  0.000000   \n",
       "82330  0.000000e+00  0.000000  0.000000  0.000002  0.000000  0.000000   \n",
       "82331  1.500000e-07  0.000094  0.000000  0.000006  0.000000  0.111111   \n",
       "\n",
       "           sttl      dttl         sload     dload  ...  service_ftp  \\\n",
       "0      0.988235  1.000000  2.364553e-06  0.000379  ...          0.0   \n",
       "1      0.243137  0.992126  1.401989e-06  0.022458  ...          0.0   \n",
       "2      0.243137  0.992126  2.625704e-07  0.002717  ...          0.0   \n",
       "3      0.243137  0.992126  4.576117e-07  0.000150  ...          1.0   \n",
       "4      0.996078  0.992126  1.429776e-06  0.000178  ...          0.0   \n",
       "...         ...       ...           ...       ...  ...          ...   \n",
       "82327  0.996078  0.000000  1.389445e-02  0.000000  ...          0.0   \n",
       "82328  0.996078  0.992126  2.072552e-05  0.000100  ...          0.0   \n",
       "82329  0.000000  0.000000  0.000000e+00  0.000000  ...          0.0   \n",
       "82330  0.000000  0.000000  0.000000e+00  0.000000  ...          0.0   \n",
       "82331  0.996078  0.000000  7.719141e-03  0.000000  ...          0.0   \n",
       "\n",
       "       service_ftp-data  service_http  service_irc  service_pop3  \\\n",
       "0                   0.0           0.0          0.0           0.0   \n",
       "1                   0.0           0.0          0.0           0.0   \n",
       "2                   0.0           0.0          0.0           0.0   \n",
       "3                   0.0           0.0          0.0           0.0   \n",
       "4                   0.0           0.0          0.0           0.0   \n",
       "...                 ...           ...          ...           ...   \n",
       "82327               0.0           0.0          0.0           0.0   \n",
       "82328               0.0           0.0          0.0           0.0   \n",
       "82329               0.0           0.0          0.0           0.0   \n",
       "82330               0.0           0.0          0.0           0.0   \n",
       "82331               0.0           0.0          0.0           0.0   \n",
       "\n",
       "       service_radius  service_smtp  service_snmp  service_ssh  service_ssl  \n",
       "0                 0.0           0.0           0.0          0.0          0.0  \n",
       "1                 0.0           0.0           0.0          0.0          0.0  \n",
       "2                 0.0           0.0           0.0          0.0          0.0  \n",
       "3                 0.0           0.0           0.0          0.0          0.0  \n",
       "4                 0.0           0.0           0.0          0.0          0.0  \n",
       "...               ...           ...           ...          ...          ...  \n",
       "82327             0.0           0.0           0.0          0.0          0.0  \n",
       "82328             0.0           0.0           0.0          0.0          0.0  \n",
       "82329             0.0           0.0           0.0          0.0          0.0  \n",
       "82330             0.0           0.0           0.0          0.0          0.0  \n",
       "82331             0.0           0.0           0.0          0.0          0.0  \n",
       "\n",
       "[257673 rows x 196 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combined_data_X = new_train_df.drop('Class', 1)\n",
    "combined_data_X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "oos_pred = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import StratifiedKFold\n",
    "from imblearn.over_sampling import RandomOverSampler\n",
    "oversample = RandomOverSampler(sampling_strategy='minority')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kfold = StratifiedKFold(n_splits=6,shuffle=True,random_state=42)\n",
    "kfold.get_n_splits(combined_data_X,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "model = Sequential()\n",
    "model.add(Convolution1D(64, kernel_size=64, padding=\"same\",activation=\"relu\",input_shape=(196, 1)))\n",
    "model.add(MaxPooling1D(pool_size=(10)))\n",
    "model.add(BatchNormalization())\n",
    "model.add(Bidirectional(LSTM(64, return_sequences=False)))\n",
    "model.add(Reshape((128, 1), input_shape = (128, )))\n",
    "model.add(MaxPooling1D(pool_size=(5)))\n",
    "model.add(BatchNormalization())\n",
    "model.add(Bidirectional(LSTM(128, return_sequences=False)))\n",
    "#model.add(Reshape((128, 1), input_shape = (128, )))\n",
    "model.add(Dropout(0.6))\n",
    "model.add(Dense(10))\n",
    "model.add(Activation('softmax'))\n",
    "model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, 196, 64)\n",
      "(None, 19, 64)\n",
      "(None, 19, 64)\n",
      "(None, 128)\n",
      "(None, 128, 1)\n",
      "(None, 25, 1)\n",
      "(None, 25, 1)\n",
      "(None, 256)\n",
      "(None, 256)\n",
      "(None, 10)\n",
      "(None, 10)\n"
     ]
    }
   ],
   "source": [
    "for layer in model.layers:\n",
    "    print(layer.output_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_3\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv1d_2 (Conv1D)            (None, 196, 64)           4160      \n",
      "_________________________________________________________________\n",
      "max_pooling1d_1 (MaxPooling1 (None, 19, 64)            0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_1 (Batch (None, 19, 64)            256       \n",
      "_________________________________________________________________\n",
      "bidirectional_1 (Bidirection (None, 128)               66048     \n",
      "_________________________________________________________________\n",
      "reshape_1 (Reshape)          (None, 128, 1)            0         \n",
      "_________________________________________________________________\n",
      "max_pooling1d_2 (MaxPooling1 (None, 25, 1)             0         \n",
      "_________________________________________________________________\n",
      "batch_normalization_2 (Batch (None, 25, 1)             4         \n",
      "_________________________________________________________________\n",
      "bidirectional_2 (Bidirection (None, 256)               133120    \n",
      "_________________________________________________________________\n",
      "dropout (Dropout)            (None, 256)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 10)                2570      \n",
      "_________________________________________________________________\n",
      "activation (Activation)      (None, 10)                0         \n",
      "=================================================================\n",
      "Total params: 206,158\n",
      "Trainable params: 206,028\n",
      "Non-trainable params: 130\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train index: [     0      1      2 ... 257669 257670 257672]\n",
      "test index: [     9     14     15 ... 257658 257668 257671]\n",
      "Normal            77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13628\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1940\n",
      "Shellcode          1259\n",
      "Worms               145\n",
      "Name: Class, dtype: int64\n",
      "Normal            77500\n",
      "Worms             77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13628\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1940\n",
      "Shellcode          1259\n",
      "Name: Class, dtype: int64\n",
      "Epoch 1/9\n",
      "9128/9128 [==============================] - 529s 57ms/step - loss: 0.5558 - accuracy: 0.7958 - val_loss: 0.5762 - val_accuracy: 0.7751\n",
      "Epoch 2/9\n",
      "9128/9128 [==============================] - 541s 59ms/step - loss: 0.4400 - accuracy: 0.8329 - val_loss: 0.5364 - val_accuracy: 0.7913\n",
      "Epoch 3/9\n",
      "9128/9128 [==============================] - 653s 71ms/step - loss: 0.4153 - accuracy: 0.8414 - val_loss: 0.5226 - val_accuracy: 0.7928\n",
      "Epoch 4/9\n",
      "9128/9128 [==============================] - 553s 61ms/step - loss: 0.4015 - accuracy: 0.8448 - val_loss: 0.5195 - val_accuracy: 0.7968\n",
      "Epoch 5/9\n",
      "9128/9128 [==============================] - 469s 51ms/step - loss: 0.3899 - accuracy: 0.8483 - val_loss: 0.5124 - val_accuracy: 0.8005\n",
      "Epoch 6/9\n",
      "9128/9128 [==============================] - 479s 52ms/step - loss: 0.3830 - accuracy: 0.8503 - val_loss: 0.5133 - val_accuracy: 0.7953\n",
      "Epoch 7/9\n",
      "9128/9128 [==============================] - 553s 61ms/step - loss: 0.3774 - accuracy: 0.8515 - val_loss: 0.4972 - val_accuracy: 0.8021\n",
      "Epoch 8/9\n",
      "9128/9128 [==============================] - 472s 52ms/step - loss: 0.3704 - accuracy: 0.8535 - val_loss: 0.5177 - val_accuracy: 0.7870\n",
      "Epoch 9/9\n",
      "9128/9128 [==============================] - 546s 60ms/step - loss: 0.3668 - accuracy: 0.8546 - val_loss: 0.5172 - val_accuracy: 0.7872\n",
      "Validation score: 0.7871745913472733\n",
      "train index: [     1      2      4 ... 257669 257670 257671]\n",
      "test index: [     0      3      7 ... 257649 257662 257672]\n",
      "Normal            77500\n",
      "Generic           49059\n",
      "Exploits          37105\n",
      "Fuzzers           20205\n",
      "DoS               13627\n",
      "Reconnaissance    11655\n",
      "Analysis           2230\n",
      "Backdoor           1941\n",
      "Shellcode          1260\n",
      "Worms               145\n",
      "Name: Class, dtype: int64\n",
      "Normal            77500\n",
      "Worms             77500\n",
      "Generic           49059\n",
      "Exploits          37105\n",
      "Fuzzers           20205\n",
      "DoS               13627\n",
      "Reconnaissance    11655\n",
      "Analysis           2230\n",
      "Backdoor           1941\n",
      "Shellcode          1260\n",
      "Name: Class, dtype: int64\n",
      "Epoch 1/9\n",
      "9128/9128 [==============================] - 559s 61ms/step - loss: 0.3650 - accuracy: 0.8552 - val_loss: 0.4786 - val_accuracy: 0.8086\n",
      "Epoch 2/9\n",
      "9128/9128 [==============================] - 545s 60ms/step - loss: 0.3593 - accuracy: 0.8569 - val_loss: 0.4684 - val_accuracy: 0.8070\n",
      "Epoch 3/9\n",
      "9128/9128 [==============================] - 523s 57ms/step - loss: 0.3555 - accuracy: 0.8572 - val_loss: 0.4743 - val_accuracy: 0.8096\n",
      "Epoch 4/9\n",
      "9128/9128 [==============================] - 535s 59ms/step - loss: 0.3523 - accuracy: 0.8584 - val_loss: 0.4616 - val_accuracy: 0.8118\n",
      "Epoch 5/9\n",
      "9128/9128 [==============================] - 511s 56ms/step - loss: 0.3500 - accuracy: 0.8592 - val_loss: 0.4744 - val_accuracy: 0.8105\n",
      "Epoch 6/9\n",
      "9128/9128 [==============================] - 552s 60ms/step - loss: 0.3488 - accuracy: 0.8596 - val_loss: 0.4624 - val_accuracy: 0.8126\n",
      "Epoch 7/9\n",
      "9128/9128 [==============================] - 2799s 307ms/step - loss: 0.3460 - accuracy: 0.8608 - val_loss: 0.4593 - val_accuracy: 0.8140\n",
      "Epoch 8/9\n",
      "9128/9128 [==============================] - 1430s 157ms/step - loss: 0.3442 - accuracy: 0.8610 - val_loss: 0.4652 - val_accuracy: 0.8120\n",
      "Epoch 9/9\n",
      "9128/9128 [==============================] - 515s 56ms/step - loss: 0.3430 - accuracy: 0.8617 - val_loss: 0.4540 - val_accuracy: 0.8146\n",
      "Validation score: 0.814627671960136\n",
      "train index: [     0      1      3 ... 257670 257671 257672]\n",
      "test index: [     2      5     12 ... 257661 257664 257666]\n",
      "Normal            77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13627\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Worms               145\n",
      "Name: Class, dtype: int64\n",
      "Normal            77500\n",
      "Worms             77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13627\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Name: Class, dtype: int64\n",
      "Epoch 1/9\n",
      "9128/9128 [==============================] - 4577s 501ms/step - loss: 0.3443 - accuracy: 0.8615 - val_loss: 0.4473 - val_accuracy: 0.8159\n",
      "Epoch 2/9\n",
      "9128/9128 [==============================] - 443s 49ms/step - loss: 0.3413 - accuracy: 0.8623 - val_loss: 0.4521 - val_accuracy: 0.8161\n",
      "Epoch 3/9\n",
      "9128/9128 [==============================] - 516s 57ms/step - loss: 0.3399 - accuracy: 0.8626 - val_loss: 0.4599 - val_accuracy: 0.8143\n",
      "Epoch 4/9\n",
      "9128/9128 [==============================] - 494s 54ms/step - loss: 0.3384 - accuracy: 0.8633 - val_loss: 0.4529 - val_accuracy: 0.8135\n",
      "Epoch 5/9\n",
      "9128/9128 [==============================] - 261s 29ms/step - loss: 0.3374 - accuracy: 0.8633 - val_loss: 0.4466 - val_accuracy: 0.8181\n",
      "Epoch 6/9\n",
      "9128/9128 [==============================] - 421s 46ms/step - loss: 0.3364 - accuracy: 0.8641 - val_loss: 0.4483 - val_accuracy: 0.8176\n",
      "Epoch 7/9\n",
      "9128/9128 [==============================] - 422s 46ms/step - loss: 0.3349 - accuracy: 0.8648 - val_loss: 0.4491 - val_accuracy: 0.8163\n",
      "Epoch 8/9\n",
      "9128/9128 [==============================] - 37956s 4s/step - loss: 0.3347 - accuracy: 0.8644 - val_loss: 0.4489 - val_accuracy: 0.8151\n",
      "Epoch 9/9\n",
      "9128/9128 [==============================] - 22943s 3s/step - loss: 0.3328 - accuracy: 0.8646 - val_loss: 0.4461 - val_accuracy: 0.8168\n",
      "Validation score: 0.8168164671913566\n",
      "train index: [     0      1      2 ... 257670 257671 257672]\n",
      "test index: [     6     13     20 ... 257657 257663 257669]\n",
      "Normal            77500\n",
      "Generic           49060\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13627\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Worms               145\n",
      "Name: Class, dtype: int64\n",
      "Normal            77500\n",
      "Worms             77500\n",
      "Generic           49060\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13627\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Name: Class, dtype: int64\n",
      "Epoch 1/9\n",
      "9128/9128 [==============================] - 8425s 923ms/step - loss: 0.3348 - accuracy: 0.8644 - val_loss: 0.4380 - val_accuracy: 0.8186\n",
      "Epoch 2/9\n",
      "9128/9128 [==============================] - 6211s 680ms/step - loss: 0.3317 - accuracy: 0.8652 - val_loss: 0.4352 - val_accuracy: 0.8183\n",
      "Epoch 3/9\n",
      "9128/9128 [==============================] - 3333s 365ms/step - loss: 0.3314 - accuracy: 0.8652 - val_loss: 0.4456 - val_accuracy: 0.8168\n",
      "Epoch 4/9\n",
      "9128/9128 [==============================] - 2931s 321ms/step - loss: 0.3300 - accuracy: 0.8654 - val_loss: 0.4409 - val_accuracy: 0.8179\n",
      "Epoch 5/9\n",
      "9128/9128 [==============================] - 4842s 531ms/step - loss: 0.3284 - accuracy: 0.8663 - val_loss: 0.4379 - val_accuracy: 0.8181\n",
      "Epoch 6/9\n",
      "9128/9128 [==============================] - 7118s 780ms/step - loss: 0.3271 - accuracy: 0.8665 - val_loss: 0.4339 - val_accuracy: 0.8188\n",
      "Epoch 7/9\n",
      "9128/9128 [==============================] - 37930s 4s/step - loss: 0.3270 - accuracy: 0.8666 - val_loss: 0.4381 - val_accuracy: 0.8183\n",
      "Epoch 8/9\n",
      "9128/9128 [==============================] - 524s 57ms/step - loss: 0.3254 - accuracy: 0.8670 - val_loss: 0.4412 - val_accuracy: 0.8170\n",
      "Epoch 9/9\n",
      "9128/9128 [==============================] - 521s 57ms/step - loss: 0.3252 - accuracy: 0.8673 - val_loss: 0.5082 - val_accuracy: 0.7953\n",
      "Validation score: 0.7953195948305972\n",
      "train index: [     0      1      2 ... 257669 257671 257672]\n",
      "test index: [     4      8     11 ... 257659 257660 257670]\n",
      "Normal            77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13628\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Worms               145\n",
      "Name: Class, dtype: int64\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Normal            77500\n",
      "Worms             77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13628\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Name: Class, dtype: int64\n",
      "Epoch 1/9\n",
      "9128/9128 [==============================] - 8670s 950ms/step - loss: 0.3268 - accuracy: 0.8673 - val_loss: 0.4311 - val_accuracy: 0.8231\n",
      "Epoch 2/9\n",
      "9128/9128 [==============================] - 13093s 1s/step - loss: 0.3250 - accuracy: 0.8673 - val_loss: 0.4385 - val_accuracy: 0.8213\n",
      "Epoch 3/9\n",
      "9128/9128 [==============================] - 409s 45ms/step - loss: 0.3235 - accuracy: 0.8679 - val_loss: 0.4335 - val_accuracy: 0.8227\n",
      "Epoch 4/9\n",
      "9128/9128 [==============================] - 510s 56ms/step - loss: 0.3230 - accuracy: 0.8678 - val_loss: 0.4338 - val_accuracy: 0.8210\n",
      "Epoch 5/9\n",
      "9128/9128 [==============================] - 502s 55ms/step - loss: 0.3219 - accuracy: 0.8684 - val_loss: 0.4355 - val_accuracy: 0.8203\n",
      "Epoch 6/9\n",
      "9128/9128 [==============================] - 499s 55ms/step - loss: 0.3213 - accuracy: 0.8679 - val_loss: 0.4306 - val_accuracy: 0.8231\n",
      "Epoch 7/9\n",
      "9128/9128 [==============================] - 5270s 577ms/step - loss: 0.3203 - accuracy: 0.8688 - val_loss: 0.4389 - val_accuracy: 0.8211\n",
      "Epoch 8/9\n",
      "9128/9128 [==============================] - 503s 55ms/step - loss: 0.3204 - accuracy: 0.8688 - val_loss: 0.4407 - val_accuracy: 0.8195\n",
      "Epoch 9/9\n",
      "9128/9128 [==============================] - 6022s 660ms/step - loss: 0.3187 - accuracy: 0.8691 - val_loss: 0.4316 - val_accuracy: 0.8213\n",
      "Validation score: 0.8213063220398183\n",
      "train index: [     0      2      3 ... 257670 257671 257672]\n",
      "test index: [     1     10     17 ... 257652 257665 257667]\n",
      "Normal            77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13628\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Worms               145\n",
      "Name: Class, dtype: int64\n",
      "Normal            77500\n",
      "Worms             77500\n",
      "Generic           49059\n",
      "Exploits          37104\n",
      "Fuzzers           20205\n",
      "DoS               13628\n",
      "Reconnaissance    11656\n",
      "Analysis           2231\n",
      "Backdoor           1941\n",
      "Shellcode          1259\n",
      "Name: Class, dtype: int64\n",
      "Epoch 1/9\n",
      "9128/9128 [==============================] - 7081s 776ms/step - loss: 0.3237 - accuracy: 0.8680 - val_loss: 0.4413 - val_accuracy: 0.8188\n",
      "Epoch 2/9\n",
      "9128/9128 [==============================] - 7201s 789ms/step - loss: 0.3234 - accuracy: 0.8678 - val_loss: 0.4306 - val_accuracy: 0.8224\n",
      "Epoch 3/9\n",
      "9128/9128 [==============================] - 6375s 699ms/step - loss: 0.3213 - accuracy: 0.8690 - val_loss: 0.4296 - val_accuracy: 0.8234\n",
      "Epoch 4/9\n",
      "9128/9128 [==============================] - 6670s 731ms/step - loss: 0.3194 - accuracy: 0.8696 - val_loss: 0.4270 - val_accuracy: 0.8229\n",
      "Epoch 5/9\n",
      "9128/9128 [==============================] - 6668s 731ms/step - loss: 0.3176 - accuracy: 0.8698 - val_loss: 0.4259 - val_accuracy: 0.8223\n",
      "Epoch 6/9\n",
      "9128/9128 [==============================] - 6681s 732ms/step - loss: 0.3167 - accuracy: 0.8705 - val_loss: 0.4259 - val_accuracy: 0.8257\n",
      "Epoch 7/9\n",
      "9128/9128 [==============================] - 6683s 732ms/step - loss: 0.3161 - accuracy: 0.8702 - val_loss: 0.4431 - val_accuracy: 0.8204\n",
      "Epoch 8/9\n",
      "9128/9128 [==============================] - 6771s 742ms/step - loss: 0.3165 - accuracy: 0.8707 - val_loss: 0.4294 - val_accuracy: 0.8232\n",
      "Epoch 9/9\n",
      "9128/9128 [==============================] - 5752s 630ms/step - loss: 0.3153 - accuracy: 0.8706 - val_loss: 0.4283 - val_accuracy: 0.8230\n",
      "Validation score: 0.82302945628129\n"
     ]
    }
   ],
   "source": [
    "for train_index, test_index in kfold.split(combined_data_X,y_train):\n",
    "    train_X, test_X = combined_data_X.iloc[train_index], combined_data_X.iloc[test_index]\n",
    "    train_y, test_y = y_train.iloc[train_index], y_train.iloc[test_index]\n",
    "    \n",
    "    print(\"train index:\",train_index)\n",
    "    print(\"test index:\",test_index)\n",
    "    print(train_y.value_counts())\n",
    "    \n",
    "    train_X_over,train_y_over= oversample.fit_resample(train_X, train_y)\n",
    "    print(train_y_over.value_counts())\n",
    "    \n",
    "    x_columns_train = new_train_df.columns.drop('Class')\n",
    "    x_train_array = train_X_over[x_columns_train].values\n",
    "    x_train_1=np.reshape(x_train_array, (x_train_array.shape[0], x_train_array.shape[1], 1))\n",
    "    \n",
    "    dummies = pd.get_dummies(train_y_over) # Classification\n",
    "    outcomes = dummies.columns\n",
    "    num_classes = len(outcomes)\n",
    "    y_train_1 = dummies.values\n",
    "    \n",
    "    x_columns_test = new_train_df.columns.drop('Class')\n",
    "    x_test_array = test_X[x_columns_test].values\n",
    "    x_test_2=np.reshape(x_test_array, (x_test_array.shape[0], x_test_array.shape[1], 1))\n",
    "    \n",
    "    dummies_test = pd.get_dummies(test_y) # Classification\n",
    "    outcomes_test = dummies_test.columns\n",
    "    num_classes = len(outcomes_test)\n",
    "    y_test_2 = dummies_test.values\n",
    "    \n",
    "   \n",
    "    model.fit(x_train_1, y_train_1,validation_data=(x_test_2,y_test_2), epochs=9)\n",
    "    \n",
    "    pred = model.predict(x_test_2)\n",
    "    pred = np.argmax(pred,axis=1)\n",
    "    y_eval = np.argmax(y_test_2,axis=1)\n",
    "    score = metrics.accuracy_score(y_eval, pred)\n",
    "    oos_pred.append(score)\n",
    "    print(\"Validation score: {}\".format(score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
